/*
 * ============================================================================
 *
 *  [THC RPG] Total HardCore RPG
 *
 *  File:          rpgadmin.inc
 *  Type:          Module
 *  Description:   Admin menu for rpg commands
 *  Author:        ArsiRC
 *
 *  Copyright (C) 2009-2011  ArsiRC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ============================================================================
 */

/**
 * This module's identifier.
 */
new Module:g_moduleRPGAdmin;

/**
 * Called when RPGAdmin is created.
 * 
 * @param hMenu     Handle to RPGAdmin.
 */
new ProjectEvent:g_evOnRPGAdminCreated;

/**
 * Called when RPGAdmin is deleted.
 * 
 * @param hMenu     Handle to RPGAdmin.
 */
new ProjectEvent:g_evOnRPGAdminDeleted;

/**
 * RPGAdmin's menu handle.
 */
new Handle:g_hRPGAdminMenu;

/**
 * RPGAdmin's button index in RPGMenu.
 */
new g_iRPGAdminRPGMenuButton;

/**
 * Register this module.
 */
RPGAdmin_Register()
{
    // Define all the module's data as layed out by enum ModuleData in project.inc.
    new moduledata[ModuleData];
    
    moduledata[ModuleData_Disabled] = false;
    moduledata[ModuleData_Hidden] = false;
    strcopy(moduledata[ModuleData_FullName], MM_DATA_FULLNAME, "RPGAdmin");
    strcopy(moduledata[ModuleData_ShortName], MM_DATA_SHORTNAME, "rpgadmin");
    strcopy(moduledata[ModuleData_Description], MM_DATA_DESCRIPTION, "Admin menu for rpg commands");
    moduledata[ModuleData_Dependencies][0] = INVALID_MODULE;
    
    // Send this array of data to the module manager.
    g_moduleRPGAdmin = ModuleMgr_Register(moduledata);
    
    // Create custom events.
    g_evOnRPGAdminCreated = EventMgr_CreateEvent("Event_OnRPGAdminCreated");
    g_evOnRPGAdminDeleted = EventMgr_CreateEvent("Event_OnRPGAdminDeleted");
    
    // Register the OnEventsRegister event to register all events in it.
    EventMgr_RegisterEvent(g_moduleRPGAdmin, "Event_OnEventsRegister",            "RPGAdmin_OnEventsRegister");
}

/**
 * Register all events here.
 */
public RPGAdmin_OnEventsRegister()
{
    // Register all the events needed for this module.
    EventMgr_RegisterEvent(g_moduleRPGAdmin, "Event_OnAllModulesLoaded",          "RPGAdmin_OnAllModulesLoaded");
    EventMgr_RegisterEvent(g_moduleRPGAdmin, "Event_OnMyModuleEnable",            "RPGAdmin_OnMyModuleEnable");
    EventMgr_RegisterEvent(g_moduleRPGAdmin, "Event_OnMyModuleDisable",           "RPGAdmin_OnMyModuleDisable");
    
    #if defined PROJECT_GAME_CSS
    
    #endif
}

/**
 * Plugin is loading.
 */
RPGAdmin_OnPluginStart()
{
    // Register the module.
    RPGAdmin_Register();
    
    // Register the RPGMenu command.
    RegConsoleCmd("rpgadmin", Command_RPGAdmin);
}

/**
 * All modules have been registered at this point.
 */
public RPGAdmin_OnAllModulesLoaded()
{
    RPGAdmin_CreateMenu();
}

/**
 * The module that hooked this event callback has been enabled.
 * 
 * @param refusalmsg    The string that is printed if Plugin_Handled is returned and it is non-empty.
 * @param maxlen        The max length of the string.
 * 
 * @return              Return Plugin_Handled to stop enable, and Plugin_Continue to allow it.
 */
public Action:RPGAdmin_OnMyModuleEnable(String:refusalmsg[], maxlen)
{
    RPGAdmin_CreateMenu();
}

/**
 * The module that hooked this event callback has been disabled.
 * 
 * @param refusalmsg    The string that is printed if Plugin_Handled is returned and it is non-empty.
 * @param maxlen        The max length of the string.
 * 
 * @return              Return Plugin_Handled to stop disable, and Plugin_Continue to allow it.
 */
public Action:RPGAdmin_OnMyModuleDisable(String:refusalmsg[], maxlen)
{
    RPGAdmin_DeleteMenu();
}

RPGAdmin_CreateMenu()
{
    // Register menu with menulib.
    g_hRPGAdminMenu = MenuLib_CreateMenu("RPGAdmin", INVALID_FUNCTION, INVALID_FUNCTION, "RPGAdmin title", true, false, false);
    
    // Forward event to all modules.
    new any:eventdata[1][1];
    eventdata[0][0] = g_hRPGAdminMenu;
    EventMgr_Forward(g_evOnRPGAdminCreated, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType2);
}

RPGAdmin_DeleteMenu()
{
    // Forward event to all modules.
    new any:eventdata[1][1];
    eventdata[0][0] = g_hRPGAdminMenu;
    EventMgr_Forward(g_evOnRPGAdminDeleted, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType2);
    
    // Delete the entire menu tree.
    MenuLib_DeleteMenu(g_hRPGAdminMenu);
}

/**
 * Command callback (RPGMenu)
 * Open up RPGMenu.
 * 
 * @param client    The client index.
 * @param argc      Argument count.
 */
public Action:Command_RPGAdmin(client, argc)
{
    // Check if the module is disabled.
    if (ModuleMgr_IsDisabled(g_moduleRPGAdmin))
        return Plugin_Continue;
    
    if (client == SERVER_INDEX)
    {
        TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Reply, INVALID_MODULE, false, "Must be player");
        return Plugin_Handled;
    }

    // Check if the client has permission to use this.
    if (!AccessMgr_HasAccess(client, g_moduleRPGAdmin))
    {
        TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Reply, INVALID_MODULE, false, "No access to command");
        return Plugin_Handled;
    }

    MenuLib_DisplayMenu(g_hRPGAdminMenu, client);
    return Plugin_Handled;
}
